package org.cloudnote.aspect;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

@Component //让spring容器识别
@Aspect //定义为切面
public class ExcptionBean {

    @AfterThrowing(throwing = "e" ,
    pointcut = "within(org.cloudnote.controller..*)")
    public void logException(Exception e){
        //记录异常信息
        //System.out.println("e = " + e);
        try {
            FileWriter out =
                    new FileWriter("cloud_note.log",true);
            PrintWriter pw = new PrintWriter(out);
            e.printStackTrace(pw);
            pw.flush();
            pw.close();
            out.close();
        } catch (IOException e1) {
            System.out.println("记录异常信息失败");
        }


    }
}
